package utils;

import java.sql.*;

//数据库连接类
public  class ConnectionManager {
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    //获得连接
    public Connection getCon() throws SQLException {
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookstore?useUnicode=true&characterEncoding=utf-8", "admin", "admin");
        return connection;
    }
    //关闭连接
    public void closeAll(Connection con,Statement ps,ResultSet rst){
        try {
            if(rst!=null){
                rst.close();
            }
            if(ps!=null){
                ps.close();
            }
            if(con!=null){
                con.close();
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("关闭连接失败！");
        }
    }
}